﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System;

namespace ScFramework.Core
{

    public abstract class LogicModuleBase
    {

            private SubmoduleBase _parent;


        /*   /// <summary>
           /// 获取该Submodule中的其他logicModule的接口,谨慎使用,可通过消息队列代替
           /// </summary>
           /// <typeparam name="T">接口</typeparam>
           /// <returns></returns>
           protected T GetModule<T>()where T:class
           {
               return _parent.Get<T>();
           }*/

        protected T GetInnerModule<T>()where T : IModuleInnerInterface
        {
            return _parent.GetInnerModule<T>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="args"></param>
        public virtual void Init(SubmoduleBase p,object args=null)
        {
#if TEST_MODULE
            Debug.Log($"{this}:begin init,parent is {p},args is{args}");
#endif
            _parent = p;

            OnInit(args);
        }

        public virtual void OnInit(object args) { }

        /// <summary>
        /// 第一次活跃时执行,比init靠后
        /// </summary>
        public virtual void OnEnable()
        {
#if TEST_MODULE
            Debug.Log($"{this}:begin OnEnable");
#endif
        }

        public virtual void Update()
        {
#if TEST_MODULE
            Debug.Log($"{this}:begin update");
#endif
        }

        public virtual void FixedUpdate()
        {
#if TEST_MODULE
            Debug.Log($"{this}:begin fixedUpdate");
#endif
        }

        public virtual void OnDisable()
        {

        }

        public virtual void LateFixedUpdate()
        {
#if TEST_MODULE
            Debug.Log($"{this}:begin lateFixedUpdate");
#endif
        }
        public virtual void OnDestroy()
        {
#if TEST_MODULE
            Debug.Log($"{this}:begin onDestory");
#endif
        }

        protected void Dispatch<T>(T id,object args = null,bool blocking=true)where T:struct
        {
            Debug.Assert(id.GetType() != typeof(int));
#if TEST_MODULE
            Debug.Log($"{this}:dispatch message {id},{args},{blocking}");
#endif
            _parent.Dispatch(typeof(T),id, args, blocking);
        }
    }


    public class LogicModuleBase<T> : LogicModuleBase where T : SystemModule
    {
        T systemModule;

        public override void Init(SubmoduleBase p, object args = null)
        {
            ___SetSystemModule(p.GetType().GetField("_main", BindingFlags.NonPublic|BindingFlags.Instance).GetValue(p) as Main);
            base.Init(p, args);
        }
        public virtual void ___SetSystemModule(Main main)
        {
            systemModule = main.Get<T>();
        }

        protected T SysModule
        {
            get { return systemModule; }
        }
        public override void OnDestroy()
        {
            base.OnDestroy();
            if(systemModule is MessageManager e)
            {
                e.RemoveAbout(this);
            }
            systemModule = null;
        }
    }
    public class LogicModuleBase<T0,T1> : LogicModuleBase<T0> where T0:SystemModule where T1:SystemModule
    {
        T1 systemModule;

        public override void ___SetSystemModule(Main main)
        {
            base.___SetSystemModule(main);
            systemModule = main.Get<T1>();
        }

        protected T1 SysModule1
        {
            get { return systemModule; }
        }
        public override void OnDestroy()
        {
            base.OnDestroy();
            if(SysModule is MessageManager e) {

                e.RemoveAbout(this);
            }
            systemModule = null;
        }
    }
}